'''
编写一个函数来查找字符串数组中的最长公共前缀。
如果不存在公共前缀，返回空字符串 ""。 
示例 1：
输入：strs = ["flower","flow","flight"]
输出："fl"
示例 2：
输入：strs = ["dog","racecar","car"]
输出：""
解释：输入不存在公共前缀。
提示：
0 <= strs.length <= 200
0 <= strs[i].length <= 200
strs[i] 仅由小写英文字母组成
'''
class Solution:
    def longestCommonPrefix(self, strs) -> str:
        if not strs:
            return ""
        for i in range(len(strs[0])):
            for string in strs[1:]:
                if i>=len(string) or string[i] != strs[0][i]:
                    return strs[0][:i]
        return strs[0]
## 解法二
class Solution1:
    def longestCommonPrefix(self, strs) -> str:
        result = ''
        MAX = 0
        MIN= min([len(string) for string in strs])
        # print(MAX)
        for i in range(MIN):
            sets = set(strings[i] for strings in strs)
            if len(sets) ==1:
                result +=sets.pop()

        return result




if __name__ =="__main__":
    solu = Solution1()
    result = solu.longestCommonPrefix(["flower","flow","flight"])
    print(result)